home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assembly / ls55+44.lha / ls50.asm < prev    next >
Encoding:
Assembly Source File  |  1993-07-18  |  7.7 KB  |  408 lines

  1. * * * * * * * * * * * * * * * * * * * * * * * * *
  2. * ls.asm                    *
  3. * v5.0                        *
  4. * Jörgen Grahn                    *
  5. *                        *
  6. * 910718 ... 921116 ... 921221 ...        *
  7. * 921231 ... 930101 ... 930711            *
  8. *                        *
  9. * * * * * * * * * * * * * * * * * * * * * * * * *
  10. *                        *
  11. * History:                    *
  12. *                        *
  13. * (...)                        *
  14. *                        *
  15. * v4.2    (11.7.93)                *
  16. *    Fixed silly, horrible reentrancy bug by *
  17. *    replacing the BSS hunk with a dynamic    *
  18. *    allocation. This made the program    *
  19. *    8 bytes smaller.            *
  20. *                        *
  21. * v4.3    (11.7.93)                *
  22. *    Fixed memory location zero bug.    Ls    *
  23. *    would try to allocate $ABADCAFE memory    *
  24. *    or something like that if MungWall was    *
  25. *    active - thanks, Carolyn!        *
  26. *                        *
  27. * v4.4    (11.7.93)                *
  28. *    Fixed another memory location zero bug,    *
  29. *    an old one this time. Ls would not list    *
  30. *    current directory if given no argument    *
  31. *    with MungWall active.            *
  32. *                        *
  33. * v5.0    (12.7.93)                *
  34. *    Changed error report when trying to do    *
  35. *    a directory listing on a file.        *
  36. *    Added hardlinks in italic and softlinks    *
  37. *    in white.                *
  38. *                        *
  39. *    (17.7.93)                *
  40. *    Redid the source slightly so that     *
  41. *    others may compile without changes.    *
  42. *    No more comments in Swedish - if other    *
  43. *    people hate comments in Swedish as much *
  44. *    as I hate comments in German, those    *
  45. *    would have meant *very* bad publicity.    *
  46. *    ;-)                    *
  47. *                        *
  48. *    My own include "offsets204.i" skipped,    *
  49. *    xrefs added instead.            *
  50. *                        *
  51. * * * * * * * * * * * * * * * * * * * * * * * * *
  52.  
  53. ABSEXECBASE        EQU 4
  54. MEMF_ANY        EQU 0
  55. SHARED_LOCK        EQU -2
  56. ERROR_DIR_NOT_FOUND    EQU 204
  57. ERROR_OBJECT_WRONG_TYPE    EQU 212
  58. ERROR_NO_MORE_ENTRIES    EQU 232
  59. SIGBREAKF_CTRL_C    EQU $00001000
  60. fib_SIZEOF        EQU 260
  61. fib_DIRENTRYTYPE    EQU 4            * <0 -> file, >=0 -> dir
  62. fib_FILENAME        EQU 8
  63.  
  64. ST_ROOT            EQU  1
  65. ST_USERDIR        EQU  2
  66. ST_SOFTLINK        EQU  3    
  67. ST_LINKDIR        EQU  4    
  68. ST_FILE            EQU -3
  69. ST_LINKFILE        EQU -4
  70. ST_PIPEFILE        EQU -5
  71.  
  72.     XREF    _LVOAllocVec
  73.     XREF    _LVOOpenLibrary
  74.     XREF    _LVOReadArgs
  75.     XREF    _LVOLock
  76.     XREF    _LVOExamine
  77.     XREF    _LVOPrintFault
  78.     XREF    _LVOCheckSignal
  79.     XREF    _LVOIoErr
  80.     XREF    _LVOUnLock
  81.     XREF    _LVOFreeArgs
  82.     XREF    _LVOCloseLibrary
  83.     XREF    _LVOFreeVec
  84.     XREF    _LVOOutput
  85.     XREF    _LVOPutStr
  86.     XREF    _LVOFlush
  87.     XREF    _LVOExNext
  88.  
  89.  
  90. libcall    MACRO
  91.     jsr _LVO\1(a6)
  92.     ENDM
  93.  
  94. * * * * * * * * * * * * * * * * * * * * * * * * *
  95.  
  96.     OFFSET    0
  97. ra_array:
  98.     ds.l    1
  99. ***    CNOP    0,4        * Why can't I CNOP here?
  100.                 * Well, it's OK anyway. FileInfoBlocks
  101.                 * need to be on a LONG-aligned address.
  102.                 * Just think twice before adding data here!
  103. fib:    ds.b    fib_SIZEOF    * FileInfoBlock
  104.     ds.w    0
  105. ***    CNOP    0,2
  106. file:    ds.b    4+4+30+4+4+1+1    * Room for:
  107.                 * a) Two Escape sequences
  108.                 * b) 30 byte filename
  109.                 * c) Two Escape sequences
  110.                 * d) one space
  111.                 * e) terminating NULL byte
  112. blocksize:
  113.  
  114. * * * * * * * * * * * * * * * * * * * * * * * * *
  115.  
  116.     SECTION "text",code
  117.  
  118. entrypoint:
  119.     movem.l    d1-d7/a0-a6,-(sp)
  120.  
  121.     move.l    ABSEXECBASE.w,a6
  122.  
  123.     move.l    #blocksize,d0
  124.     move.l    #MEMF_ANY,d1
  125.     libcall    AllocVec            * Allocate memory
  126.     move.l    d0,a4                * to play with...
  127.     beq    exit
  128.  
  129.     lea    dosname(pc),a1
  130.     moveq    #36,d0
  131.  
  132.     libcall    OpenLibrary
  133.     move.l    d0,a6
  134.     beq    freemem
  135.  
  136.     * We now have DosBase in a6, and "BSS" in a4.
  137.  
  138.     lea    ra_array(a4),a0
  139.     lea.l    zerostring(pc),a1        * Point the argument to fill
  140.     move.l    a1,(a0)                * in to a zero lenght string
  141.     move.l    a0,d2                * to be used as default.
  142.     lea    template(pc),a0
  143.     move.l    a0,d1
  144.     clr.l    d3
  145.  
  146.     libcall    ReadArgs
  147.     move.l    d0,a5                * Save the ReadArgs struct
  148.     bne.s    readargs_ok
  149.  
  150.     bsr    printioerr
  151.     bra    closedos
  152.  
  153. readargs_ok:                    * Dos in a6, ReadArgs in a5.
  154.  
  155.     move.l    ra_array(a4),d1            * Program argument string
  156.     move.l    #SHARED_LOCK,d2
  157.     libcall    Lock
  158.     move.l    d0,d7
  159.     bne.s    locked_ok
  160.  
  161.     bsr    printioerr
  162.     bra.s    freeargs
  163.  
  164. locked_ok:                * Dos - a6    ReadArgs - a5
  165.                     * Lock - d7    "BSS" - a4
  166.     lea    fib(a4),a3        * fib - a3
  167.  
  168.     move.l    d7,d1
  169.     move.l    a3,d2
  170.     libcall    Examine
  171.     tst.l    d0
  172.     beq.s    examine_failed
  173.     tst.l    fib_DIRENTRYTYPE(a3)    * Did we Lock() a file?
  174.     bpl.s    dir_examined
  175.  
  176. examine_on_file:
  177.     moveq    #0,d2
  178.     move.l    #ERROR_OBJECT_WRONG_TYPE,d1
  179.     libcall    PrintFault
  180.     bra.s    freelock
  181.  
  182.     * d3 holds how many names have been written to this row.
  183.     * d4 holds the _real_ position on the row.
  184. dir_examined:
  185.     clr.l    d3
  186.     move.l    d3,d4
  187.  
  188. exnext:
  189.     lea    file(a4),a2
  190.     bsr    readname        * fib (a3) file (a2) lock (d7)
  191.                     * nr_names (d3) pos (d4)
  192.     tst.l    d0
  193.     bne.s    print_file
  194.     bsr.s    newline            * Put us on new line...
  195.     bra.s    no_more_files        * ...and close down
  196.  
  197. print_file:
  198.     move.l    a2,a0
  199.     bsr.s    putstr
  200.  
  201.     move.l    #SIGBREAKF_CTRL_C,d1
  202.     libcall    CheckSignal        * Check for ^C.
  203.     tst.l    d0
  204.     beq.s    exnext
  205.  
  206.     bsr.s    newline            * Put us on new line...
  207.     lea    breakstring(pc),a0
  208.     bsr.s    putstr
  209.     bra.s    freelock
  210.  
  211. examine_failed:
  212. no_more_files:
  213.     libcall    IoErr            * Should an error be printed?
  214.     cmpi.l    #ERROR_NO_MORE_ENTRIES,d0
  215.     beq.s    freelock
  216.     bsr    printioerr
  217.  
  218. freelock:
  219.     move.l    d7,d1
  220.     libcall    UnLock
  221.  
  222. freeargs:
  223.     move.l    a5,d1
  224.     libcall    FreeArgs
  225.  
  226. closedos:
  227.     move.l    a6,a1
  228.     move.l    ABSEXECBASE.w,a6
  229.     libcall    CloseLibrary
  230.  
  231. freemem:
  232.     move.l    a4,a1
  233.     move.l    ABSEXECBASE.w,a6
  234.     libcall    FreeVec
  235.  
  236. exit:
  237.     moveq    #0,d0
  238.     movem.l    (sp)+,d1-d7/a0-a6
  239.     rts
  240.  
  241. * * * * * * * * * * * * * * * * * * * * * * * * *
  242. *
  243. * Puts us on a new line, if necessary.
  244. * Destroys d0-d2/a0-a1.
  245.  
  246. newline:
  247.     tst.l    d4
  248.     beq.s    1$
  249.     lea    newlinestr(pc),a0
  250.     bsr.s    putstr
  251. 1$:    rts
  252.  
  253. * * * * * * * * * * * * * * * * * * * * * * * * *
  254. *
  255. * fputs(string (a0))
  256. * Destroys d0-d1/d5/a0-a1
  257. *
  258.  
  259. putstr:
  260.     move.l    a0,-(sp)
  261.     libcall    Output
  262.     move.l    d0,d5
  263.     beq.s    1$
  264.     move.l    (sp)+,d1
  265.     libcall    PutStr
  266.     move.l    d5,d1
  267.     libcall    Flush
  268. 1$:    rts
  269.  
  270. * * * * * * * * * * * * * * * * * * * * * * * * *
  271. *
  272. * Reads a filename for printing.
  273. *
  274. * a2 - 'file' array
  275. * a3 - fib
  276. * d7 - lock
  277. * d3 - number of names previous on row (altered [0..3])
  278. * d4 - position on row (altered)
  279. *
  280. * returns 0 if end of names.
  281.  
  282. readname:
  283.     movem.l    d1-d2/d5-d7/a0-a6,-(sp)
  284.  
  285.     move.l    d7,d1
  286.     move.l    a3,d2
  287.     libcall    ExNext
  288.     tst.l    d0
  289.     beq    sorry
  290.  
  291.     move.l    a2,a1                * a2 - name buffer (aligned)
  292.     move.l    fib_DIRENTRYTYPE(a3),d1        * Type in d1.
  293.  
  294.     * Following is conditional code to print different
  295.     * types of files in different ways.
  296.     * Softlinks:    in white
  297.     * Hardlinks:    italic
  298.     * Directories:    blue
  299.     * Files:    black
  300.     * Other things:    black
  301.  
  302.     bmi    isfiletype
  303.     * A softlink, a hardlink to directory, a directory, or something else.
  304.     cmpi.w    #ST_SOFTLINK,d1
  305.     bne.s    1$
  306.     lea    csifaint(pc),a5
  307.     bsr    esc_cpy
  308.     bsr.s    fill_name
  309.     lea    csicolclr(pc),a5
  310.     bsr    esc_cpy
  311.     bra.s    endprint
  312. 1$:    cmpi.w    #ST_LINKDIR,d1
  313.     bne.s    2$
  314.     lea    csihigh(pc),a5
  315.     bsr.s    esc_cpy
  316.     lea    csiital(pc),a5
  317.     bsr.s    esc_cpy
  318.     bsr.s    fill_name
  319.     lea    csicolclr(pc),a5
  320.     bsr.s    esc_cpy
  321.     lea    csiioff(pc),a5
  322.     bsr.s    esc_cpy
  323.     bra.s    endprint
  324. 2$:
  325.     lea    csihigh(pc),a5
  326.     bsr.s    esc_cpy
  327.     bsr.s    fill_name
  328.     lea    csicolclr(pc),a5
  329.     bsr.s    esc_cpy
  330.     bra.s    endprint
  331. isfiletype:
  332.     * A file, a hardlink or something else.
  333.     cmpi.w    #ST_LINKFILE,d1
  334.     bne.s    1$
  335.     lea    csiital(pc),a5
  336.     bsr.s    esc_cpy
  337.     bsr.s    fill_name
  338.     lea    csiioff(pc),a5
  339.     bsr.s    esc_cpy
  340.     bra.s    endprint
  341. 1$:
  342.     bsr.s    fill_name
  343. endprint:
  344.     
  345. fill_spaces:
  346.     move.b    positions(pc,d3),d0
  347. 1$:    move.b    #' ',(a1)+
  348.     addq.l    #1,d4
  349.     cmp.b    d0,d4
  350.     ble.s    1$
  351.  
  352. done_spaces:
  353.     addq.l    #1,d3
  354.     cmpi.w    #4,d3
  355.     bne.s    1$
  356.  
  357.     clr.l    d3
  358.     move.l    d3,d4
  359.     move.b    #10,(a1)+
  360. 1$:    move.b    #0,(a1)            * D0 should here be non-zero.
  361.  
  362. sorry:    movem.l    (sp)+,d1-d2/d5-d7/a0-a6
  363.     rts
  364.  
  365. fill_name:
  366.     lea    fib_FILENAME(a3),a0
  367. 2$:    move.b    (a0)+,d0
  368.     beq.s    1$
  369.     addq.l    #1,d4
  370.     move.b    d0,(a1)+
  371.     bra.s    2$
  372. 1$:    rts
  373.  
  374. esc_cpy:
  375. 1$:    move.b    (a5)+,(a1)+
  376.     bne.s    1$
  377.     lea    -1(a1),a1
  378.     rts
  379.  
  380. * * * * * * * * * * * * * * * * * * * * * * * * *
  381. *
  382. * Prints IoErr().
  383. * Destroys a0-a1/d0-d2.
  384. *
  385. printioerr:
  386.     libcall    IoErr
  387.     moveq    #0,d2
  388.     move.l    d0,d1
  389.     libcall    PrintFault
  390.     rts
  391.  
  392. * * * * * * * * * * * * * * * * * * * * * * * * *
  393.  
  394. positions:    dc.b    16,32,48,64
  395. template:    dc.b    'DIR',0
  396. dosname:    dc.b    'dos.library',0
  397. versionstring:    dc.b    '$VER: ls 5.0 (12.7.93) Jörgen Grahn',0
  398. breakstring:    dc.b    '***Break'
  399. newlinestr:    dc.b    10
  400. zerostring:    dc.b    0
  401. escapesequences:
  402. csihigh:    dc.b    $9b,'33m',0    * Highlight (blue)
  403. csifaint:    dc.b    $9b,'2m',0    * Faint (white)
  404. csicolclr:    dc.b    $9b,'39m',0    * Reset colour
  405. csiital:    dc.b    $9b,'3m',0    * Italic
  406. csiioff:    dc.b    $9b,'23m',0    * Italic off
  407.     END
  408.